package com.plan.action.welcome;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plan.dao.logindao.LoginDao;
import com.plan.login.entity.LoginEntity;
import com.plan.login.verify.makeCertPic;

/**
 * 首页
 * 
 *
 * @author wangzih
 */
@Controller
@RequestMapping("/index")
public class ActionIndex {
	private String NAV_TEXT_FILE="src/com/config/nav-text.xml";
	@Autowired
	private LoginDao dao;
	Logger logger=Logger.getLogger(this.getClass());
	@RequestMapping("/index.do")
	public String toIndex(){
		return "plan/index/index";
	}
	
	@RequestMapping("/navtext.do")
	@ResponseBody
	public void getNavText(HttpSession session,HttpServletResponse res) throws Exception{
		PrintWriter out = res.getWriter();
		try{
			File xmlfile=new File(NAV_TEXT_FILE);
			SAXReader reader=new SAXReader();
			Document doc = reader.read(xmlfile);
			out.println(doc.asXML());
		}finally{
			out.close();
		}
	}
	
	@RequestMapping("/login.do")
	@ResponseBody
	public void login(HttpServletRequest req,HttpServletResponse res) throws Exception{
		String code=req.getParameter("code");
		JSONObject obj=new JSONObject();
		String vercode = (String)req.getSession().getAttribute("verifycode");
		obj.put("state", "false");
		if(code==null || !code.equalsIgnoreCase(vercode)){
			obj.put("msg", "验证码错误");
			writeAJAXmsg(obj.toString(), res);
			return;
		}
		String id=req.getParameter("id");
		String pwd=req.getParameter("pwd");
		LoginEntity le = dao.findUser(id);
		if(le==null||pwd==null||!pwd.equals(le.getPwd())){
			obj.put("msg", "用户名或密码错误");
			logger.info("用户"+id+"密码错误");
			writeAJAXmsg(obj.toString(), res);
			return;
		}
			req.getSession().setAttribute("id", id);
			obj.put("state", "true");
			logger.info("用户"+id+"登录成功");
			writeAJAXmsg(obj.toString(), res);
	}
	
	@RequestMapping("/checklogin")
	@ResponseBody
	public void checkLogin(HttpSession session,HttpServletResponse res) throws Exception{
		PrintWriter out = res.getWriter();
		try{
			String username = (String) session.getAttribute("id");
			if(username==null){
				out.print("false");
			}else{
				JSONObject obj=new JSONObject();
				obj.put("name", username);
				out.print(obj.toString());
			}
		}finally{
			out.close();
		}
	}
	
	@RequestMapping("/reg")
	@ResponseBody
	public void reg(HttpServletRequest req,HttpServletResponse res) throws Exception {
		String code=req.getParameter("code");
		String vercode = (String)req.getSession().getAttribute("verifycode");
		if(code==null || !code.equalsIgnoreCase(vercode)){
			writeAJAXmsg("验证码错误", res);
			return;
		}
		String id = (String) req.getParameter("id");
		String pwd=(String )req.getParameter("pwd");
		LoginEntity le=new LoginEntity();
		le.setId(id);
		le.setPwd(pwd);
		try {
			dao.addUser(le);
			req.getSession().setAttribute("id", le.getId());
			logger.info("用户"+id+"注册并登录");
			writeAJAXmsg("success", res);
		} catch (Exception e) {
			e.printStackTrace();
			writeAJAXmsg(e.getCause()+"", res);
		}
		
	}
	@RequestMapping("/loginout")
	@ResponseBody
	public void loginout(HttpSession session,HttpServletResponse res) throws Exception{
		String id =(String) session.getAttribute("id");
		session.removeAttribute("id");
		logger.info("用户"+id+"注销");
		writeAJAXmsg("success", res);
	}
	@RequestMapping("/verify.do")
	@ResponseBody
	public void getVerifyImg(HttpSession session,HttpServletResponse res) throws Exception{
		ServletOutputStream out = res.getOutputStream();
		try{
			res.setContentType("image/jpg");
			String certPic = makeCertPic.getCertPic(0, 0, out);
			session.setAttribute("verifycode", certPic);
		}finally{
			out.close();
		}
	}
	
	@RequestMapping("/checkid.do")
	@ResponseBody
	public void checkId(HttpServletRequest req,HttpServletResponse res) throws Exception{
		String id = req.getParameter("id");
		LoginEntity le = dao.findUser(id);
		JSONObject obj=new JSONObject();
		if(le==null){
			obj.put("state", "true");
		}else{
			obj.put("state", "false");
		}
		writeAJAXmsg(obj.toString(),res);
	}
	
	private void writeAJAXmsg(String msg,HttpServletResponse res) throws Exception{
		PrintWriter out = res.getWriter();
		try{
			out.print(msg);
		}finally{
			out.close();
		}
	}
}
